<!--
Copyright (C) 2009 Apple Computer, Inc.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<html>
<head>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"></script>
<script src="../resources/desktop-gl-constants.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>

<script>
description("Test for invalid passed parameters");

var context = create3DContext();

debug("");
debug("Test createShader()");
shouldGenerateGLError(context, context.NO_ERROR, "context.createShader(context.FRAGMENT_SHADER)");
shouldGenerateGLError(context, context.NO_ERROR, "context.createShader(context.VERTEX_SHADER)");
shouldGenerateGLError(context, context.INVALID_ENUM, "context.createShader(0)");
shouldGenerateGLError(context, context.INVALID_ENUM, "context.createShader(context.TRIANGLES)");

debug("");
debug("Test clear()");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.clear(desktopGL['ACCUM_BUFFER_BIT'])");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.clear(desktopGL['ACCUM_BUFFER_BIT'] | context.COLOR_BUFFER_BIT)");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.clear(desktopGL['ACCUM_BUFFER_BIT'] | context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT | context.STENCIL_BUFFER_BIT)");
shouldGenerateGLError(context, context.NO_ERROR, "context.clear(context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT | context.STENCIL_BUFFER_BIT)");

debug("");
debug("Test bufferData()");
var buffer = context.createBuffer();
shouldGenerateGLError(context, context.NO_ERROR, "context.bindBuffer(context.ARRAY_BUFFER, buffer)");
shouldGenerateGLError(context, context.NO_ERROR, "context.bufferData(context.ARRAY_BUFFER, 16, context.STREAM_DRAW)");
shouldGenerateGLError(context, context.NO_ERROR, "context.bufferData(context.ARRAY_BUFFER, 16, context.STATIC_DRAW)");
shouldGenerateGLError(context, context.NO_ERROR, "context.bufferData(context.ARRAY_BUFFER, 16, context.DYNAMIC_DRAW)");
shouldGenerateGLError(context, context.INVALID_ENUM, "context.bufferData(context.ARRAY_BUFFER, 16, desktopGL['STREAM_READ'])");
shouldGenerateGLError(context, context.INVALID_ENUM, "context.bufferData(context.ARRAY_BUFFER, 16, desktopGL['STREAM_COPY'])");
shouldGenerateGLError(context, context.INVALID_ENUM, "context.bufferData(context.ARRAY_BUFFER, 16, desktopGL['STATIC_READ'])");
shouldGenerateGLError(context, context.INVALID_ENUM, "context.bufferData(context.ARRAY_BUFFER, 16, desktopGL['STATIC_COPY'])");
shouldGenerateGLError(context, context.INVALID_ENUM, "context.bufferData(context.ARRAY_BUFFER, 16, desktopGL['DYNAMIC_READ'])");
shouldGenerateGLError(context, context.INVALID_ENUM, "context.bufferData(context.ARRAY_BUFFER, 16, desktopGL['DYNAMIC_COPY'])");

debug("");
debug("Test {copy}Tex{Sub}Image2D with negative offset/width/height");
var tex = context.createTexture();
var pixels = new Uint8Array(2 * 2 * 4);
shouldGenerateGLError(context, context.NO_ERROR, "context.bindTexture(context.TEXTURE_2D, tex)");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.texImage2D(context.TEXTURE_2D, 0, context.RGBA, -16, -16, 0, context.RGBA, context.UNSIGNED_BYTE, null)");
shouldGenerateGLError(context, context.NO_ERROR, "context.texImage2D(context.TEXTURE_2D, 0, context.RGBA, 16, 16, 0, context.RGBA, context.UNSIGNED_BYTE, null)");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.texSubImage2D(context.TEXTURE_2D, 0, -1, -1, 2, 2, context.RGBA, context.UNSIGNED_BYTE, pixels)");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.texSubImage2D(context.TEXTURE_2D, 0, 0, 0, -1, -1, context.RGBA, context.UNSIGNED_BYTE, pixels)");
shouldGenerateGLError(context, context.NO_ERROR, "context.texSubImage2D(context.TEXTURE_2D, 0, 0, 0, 2, 2, context.RGBA, context.UNSIGNED_BYTE, pixels)");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.copyTexImage2D(context.TEXTURE_2D, 0, context.RGBA, 0, 0, -1, -1, 0)");
shouldGenerateGLError(context, context.NO_ERROR, "context.copyTexImage2D(context.TEXTURE_2D, 0, context.RGBA, 0, 0, 16, 16, 0)");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.copyTexSubImage2D(context.TEXTURE_2D, 0, -1, -1, 0, 0, 2, 2)");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.copyTexSubImage2D(context.TEXTURE_2D, 0, 0, 0, 0, 0, -1, -1)");
shouldGenerateGLError(context, context.NO_ERROR, "context.copyTexSubImage2D(context.TEXTURE_2D, 0, 0, 0, 0, 0, 2, 2)");

debug("");
debug("Test renderbufferStorage() with negative width/height");
var renderbuffer = context.createRenderbuffer();
shouldGenerateGLError(context, context.NO_ERROR, "context.bindRenderbuffer(context.RENDERBUFFER, renderbuffer)");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.renderbufferStorage(context.RENDERBUFFER, context.RGBA4, -2, -2)");
shouldGenerateGLError(context, context.NO_ERROR, "context.renderbufferStorage(context.RENDERBUFFER, context.RGBA4, 16, 16)");

debug("");
debug("Test scissor() with negative width/height");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.scissor(0, 0, -2, -2)");
shouldGenerateGLError(context, context.NO_ERROR, "context.scissor(0, 0, 16, 16)");

debug("");
debug("Test viewport() with negative width/height");
shouldGenerateGLError(context, context.INVALID_VALUE, "context.viewport(0, 0, -2, -2)");
shouldGenerateGLError(context, context.NO_ERROR, "context.viewport(0, 0, 16, 16)");

debug("");
debug("Set up a program to test invalid characters");
var invalidSet = ['"', '$', '`', '@', '\\', "'"];
var validUniformName = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890";
var validAttribName = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
function generateShaderSource(opt_invalidIdentifierChar, opt_invalidCommentChar) {
  var invalidIdentifierString = "";
  var invalidCommentString = "";
  if (opt_invalidIdentifierChar != undefined) {
    invalidIdentifierString += opt_invalidIdentifierChar;
  }
  if (opt_invalidCommentChar != undefined) {
    invalidCommentString += opt_invalidCommentChar;
  }
  return "uniform float " + validUniformName + invalidIdentifierString + ";\n"
                          + "varying float " + validAttribName + ";\n"
                          + "void main() {\n"
                          + validAttribName  + " = " + validUniformName + ";\n"
                          + "gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }\n";
                          + "//.+-/*%<>[](){}^|&~=!:;,?# " + invalidCommentString;
}
var vShader = context.createShader(context.VERTEX_SHADER);
context.shaderSource(vShader, generateShaderSource());
context.compileShader(vShader);
shouldBe("context.getError()", "context.NO_ERROR");
var fShader = context.createShader(context.FRAGMENT_SHADER);
context.shaderSource(fShader, "precision highp float;\n"
                              + "varying float " + validAttribName + ";\n"
                              + "void main() {\n"
                              + "gl_FragColor = vec4(" + validAttribName + ", 0.0, 0.0, 1.0); }");
context.compileShader(fShader);
shouldBe("context.getError()", "context.NO_ERROR");
var program = context.createProgram();
context.attachShader(program, vShader);
context.attachShader(program, fShader);
context.linkProgram(program);
shouldBeTrue("context.getProgramParameter(program, context.LINK_STATUS)");
shouldBe("context.getError()", "context.NO_ERROR");
context.bindAttribLocation(program, 1, validAttribName);
shouldBe("context.getError()", "context.NO_ERROR");
context.getAttribLocation(program, validAttribName);
shouldBe("context.getError()", "context.NO_ERROR");
context.getUniformLocation(program, validUniformName);
shouldBe("context.getError()", "context.NO_ERROR");

debug("");
debug("Test shaderSource() with invalid characters");
for (var i = 0; i < invalidSet.length; ++i) {
  var validShaderSource = generateShaderSource(undefined, invalidSet[i]);
  context.shaderSource(vShader, validShaderSource);
  shouldBe("context.getError()", "context.NO_ERROR");
  var invalidShaderSource = generateShaderSource(invalidSet[i], undefined);
  context.shaderSource(vShader, invalidShaderSource);
  shouldBe("context.getError()", "context.INVALID_VALUE");
}

debug("");
debug("Test bindAttribLocation() with invalid characters");
for (var i = 0; i < invalidSet.length; ++i) {
  var invalidName = validAttribName + invalidSet[i];
  context.bindAttribLocation(program, 1, invalidName);
  shouldBe("context.getError()", "context.INVALID_VALUE");
}

debug("");
debug("Test getAttribLocation() with invalid characters");
for (var i = 0; i < invalidSet.length; ++i) {
  var invalidName = validAttribName + invalidSet[i];
  context.getAttribLocation(program, invalidName);
  shouldBe("context.getError()", "context.INVALID_VALUE");
}

debug("");
debug("Test getUniformLocation() with invalid characters");
for (var i = 0; i < invalidSet.length; ++i) {
  var invalidName = validUniformName + invalidSet[i];
  context.getUniformLocation(program, invalidName);
  shouldBe("context.getError()", "context.INVALID_VALUE");
}

debug("")
successfullyParsed = true;
</script>

<script src="../resources/js-test-post.js"></script>
</body>
</html>
